#include <typedefs.h>
#include <error.h>
#include <p_wkfile.h>
#include <p_status.h>
#include <pcb_defs.h>
#include <list_mgmt.h>
#include <keyboard.h>

#include "proto.h"
#include "p_tcomp.h"


#define NUM_AUX_TYPES 1
#define AUX_EEPROM_SIZE 256 
#define EEPROM_DEF_SIZE  27

#define D_DT DEFAULT_DT
#define D_DV (U_INT16)DEFAULT_DV
#define D_25 DEFAULT_V25C
#define V_AUX REQUIRED_AUX_VERSION



const struct { U_INT16 value; BIT8 definition[EEPROM_DEF_SIZE]; }  Aux_EEPROM_Default[AUX_EEPROM_SIZE] = {
/*#!inc_addr*/

  /* standard header */

  /*       ----+----1----+----2----+-               ----+----1----+----2----+-               ----+----1----+----2----+-  */
{ 0x0000, "00 Module Checksum        " },{ V_AUX,  "01 Version   U.F          " },{ 0x0000, "02 Module Type            " },
{ 0xc282, "03 Build Date             " },{ 0x0000, "04 Serial Number High     " },{ 0x0001, "05 Serial Number Low      " },
{ D_DV,   "06 Thermistor delta volts " },{ D_DT,   "07 Thermistor delta temp  " },{ D_25,   "08 Thermistor V 25C       " },

   /* sensor definitions  */
  
{ 0xa854, "09 P Sensor Range     Aux3" },{ 0xa854, "0a P Sensor Range     Aux4" },{ 0xa854, "0b P Sensor Range     Aux5" },
{ 0x3333, "0c Fact PS 25C Offset Aux3" },{ 0x3333, "0d Fact PS 25C Offset Aux4" },{ 0x3333, "0e Fact PS 25C Offset Aux5" },
{ 0x0000, "0f Fact PS dOffset/dT Aux3" },{ 0x0000, "10 Fact PS dOffset/dT Aux4" },{ 0x0000, "11 Fact PS dOffset/dT Aux5" },
{ 0x9999, "12 Fact PS Span       Aux3" },{ 0x9999, "13 Fact PS Span       Aux4" },{ 0x9999, "14 Fact PS Span       Aux5" },
{ 0x0000, "15 Fact PS dSpan/dT   Aux3" },{ 0x0000, "16 Fact PS dSpan/dT   Aux4" },{ 0x0000, "17 Fact PS dSpan/dT   Aux5" },

   /* internal unit scalings */

{ 0x00a0, "18 Pres Scaling d/cm2 Aux3" },{ 0x00a0, "19 Pres Scaling d/cm2 Aux4" },{ 0x00a0, "   Pres Scaling d/cm2 Aux5" },
{ 0x0012, "1b                        " },{ 0x0012, "1c                        " },{ 0x0012, "1d                        " },

   /* user calib  */

{ 0x0000, "1e User Calib State       " },{ 0x0000, "1f User Calib Date1       " },{ 0x0000, "20 User Calib Date2       " },
{ 0x0000, "21 User F Calib OffsetAux3" },{ 0x0000, "22 User F Calib OffsetAux4" },{ 0x0000, "23 User F Calib OffsetAux5" },
{ 0x4000, "24 User F Calib Gain  Aux3" },{ 0x4000, "25 User F Calib Gain  Aux4" },{ 0x4000, "26 User F Calib Gain  Aux5" },
{ 0x0000, "27 User F Calib Pres  Aux3" },{ 0x0000, "28 User F Calib Flow  Aux4" },{ 0x0000, "29 User F Calib Flow  Aux5" },

   /* factory calib  */

{ 0x4000, "2a Fact PS Pres Gain  Aux3" },{ 0x4000, "2b Fact PS Pres Gain  Aux4" },{ 0x4000, "2c Fact PS Pres Gain  Aux5" },
{ 0x0000, "2d Fact P Calib OffsetAux3" },{ 0x0000, "2e Fact P Calib OffsetAux4" },{ 0x0000, "2f Fact P Calib OffsetAux5" },

   /* factory pid terms */

{ 0xffff, "30 Fact   N2      Aux3  I " },{ 0xffff, "31 Fact   N2/ Air Aux4  I " },{ 0xffff, "32 Fact   N2      Aux5  I " },
{ 0xffff, "33 Fact   N2      Aux3  P " },{ 0xffff, "34 Fact   N2/ Air Aux4  P " },{ 0xffff, "35 Fact   N2      Aux5  P " },
{ 0xffff, "36 Fact   N2      Aux3  D " },{ 0xffff, "37 Fact   N2/ Air Aux4  D " },{ 0xffff, "38 Fact   N2      Aux5  D " },
{ 0xffff, "39 Fact   N2      Aux3 NL " },{ 0xffff, "3a Fact   N2/ Air Aux4 NL " },{ 0xffff, "3b Fact   N2      Aux5 NL " },

{ 0xffff, "3c Fact   H2      Aux3  I " },{ 0xffff, "3d Fact   H2      Aux4  I " },{ 0xffff, "3e Fact   H2      Aux5  I " },
{ 0xffff, "3f Fact   H2      Aux3  P " },{ 0xffff, "40 Fact   H2      Aux4  P " },{ 0xffff, "41 Fact   H2      Aux5  P " },
{ 0xffff, "42 Fact   H2      Aux3  D " },{ 0xffff, "43 Fact   H2      Aux4  D " },{ 0xffff, "44 Fact   H2      Aux5  D " },
{ 0xffff, "45 Fact   H2      Aux3 NL " },{ 0xffff, "46 Fact   H2      Aux4 NL " },{ 0xffff, "47 Fact   H2      Aux5 NL " },

{ 0xffff, "48 Fact   He      Aux3  I " },{ 0xffff, "49 Fact   He/  O2 Aux4  I " },{ 0xffff, "4a Fact   He      Aux5  I " },
{ 0xffff, "4b Fact   He      Aux3  P " },{ 0xffff, "4c Fact   He/  O2 Aux4  P " },{ 0xffff, "4d Fact   He      Aux5  P " },
{ 0xffff, "4e Fact   He      Aux3  D " },{ 0xffff, "4f Fact   He/  O2 Aux4  D " },{ 0xffff, "50 Fact   He      Aux5  D " },
{ 0xffff, "51 Fact   He      Aux3 NL " },{ 0xffff, "52 Fact   He/  O2 Aux4 NL " },{ 0xffff, "53 Fact   He      Aux5 NL " },

{ 0xffff, "54 Fact ArMe      Aux3  I " },{ 0xffff, "55 Fact ArMe/  Ar Aux4  I " },{ 0xffff, "56 Fact ArMe/  Ar Aux5  I " },
{ 0xffff, "57 Fact ArMe      Aux3  P " },{ 0xffff, "58 Fact ArMe/  Ar Aux4  P " },{ 0xffff, "59 Fact ArMe/  Ar Aux5  P " },
{ 0xffff, "5a Fact ArMe      Aux3  D " },{ 0xffff, "5b Fact ArMe/  Ar Aux4  D " },{ 0xffff, "5c Fact ArMe/  Ar Aux5  D " },
{ 0xffff, "5d Fact ArMe      Aux3 NL " },{ 0xffff, "5e Fact ArMe/  Ar Aux4 NL " },{ 0xffff, "5f Fact ArMe/  Ar Aux5 NL " },

   /* Prior to GowMac OEM det support ,i.e. to including A.03.03 , 60h...8fh = user pids; now as shown below  */

{ 0xffff, "60 CTL5_Label_chars1_2    " },{ 0xffff, "61 CTL5_Label_chars3_4    " },{ 0xffff, "62 CTL5_Label_chars5_6    " },
{ 0xffff, "63 CTL5_Label_chars7_8    " },{ 0xffff, "64 CTL5_Label_chars9_10   " },{ 0xffff, "65 CTL5_Label_chars11_12  " },
{ 0xffff, "66 XTL6_Label_chars1_2    " },{ 0xffff, "67 XTL6_Label_chars3_4    " },{ 0xffff, "68 XTL6_Label_chars5_6    " },
{ 0xffff, "69 XTL6_Label_chars7-8    " },{ 0xffff, "6a XTL6_Label_chars9_10   " },{ 0xffff, "6b XTL6_Label_chars11_12  " },

{ 0xffff, "6c XTL7_Label_chars1_2    " },{ 0xffff, "6d XTL7_Label_chars3_4    " },{ 0xffff, "6e XTL7_Label_chars5_6    " },
{ 0xffff, "6f XTL7_Label_chars7-8    " },{ 0xffff, "70 XTL7_Label_chars9_10   " },{ 0xffff, "71 XTL7_Label_chars11_12  " },
{ 0xffff, "72 CTL1_2_PULSE_Time      " },{ 0xffff, "73 CTL3_4_PULSE_Time      " },{ 0xffff, "74 CTL5_PULSE_Time        " },
{ 0xffff, "75 OEM_GC_LABEL_chars1_2  " },{ 0xffff, "76 OEM_GC_LABEL_chars3_4  " },{ 0xffff, "77 OEM_GC_LABEL_chars5_6  " },

{ 0xffff, "78 OEM_GC_LABEL_chars7_8  " },{ 0xffff, "79 OEM_GC_LABEL_chars9_10 " },{ 0xffff, "7a OEM_GC_LABEL_chars11_12" },
{ 0xffff, "7b OEM_GC_LABEL_chars13_14" },{ 0xffff, "7c OEM_GC_LABEL_chars15_16" },{ 0xffff, "7d OEM_GC_LABEL_chars17_18" },
{ 0xffff, "7e OEM_GC_LABEL_chars19_20" },{ 0xffff, "7f OEMDiag1LabelChars1_2  " },{ 0xffff, "80 OEMDiag1LabelChars3_4  " },
{ 0xffff, "81 OEMDiag1LabelChars5_6  " },{ 0xffff, "82 OEMDiag1LabelChars7_8  " },{ 0xffff, "83 OEMDiag1LabelChars9_10 " },

{ 0xffff, "84 OEMDiag1LabelCHars11_12" },{ 0xffff, "85 OEMDiag2LabelChars1_2  " },{ 0xffff, "86 OEMDiag2LabelChars3_4  " },
{ 0xffff, "87 OEMDiag2LabelChars5_6  " },{ 0xffff, "88 OEMDiag2LabelChars7_8  " },{ 0xffff, "89 OEMDiag2LabelChars9_10 " },
{ 0xffff, "8a OEMDiag2LabelCHars11_12" },{ 0xffff, "8b                        " },{ 0xffff, "8c                        " },
{ 0xffff, "8d                        " },{ 0xffff, "8e                        " },{ 0xffff, "8f                        " },

           /*      90h...0feh = unused for pure AUX module  -OR-                                      */
           /*                 = frit parameters,etc for AUX module used as OEM det pneu module        */

{ 0x0000, "90 Fuel H2 Frit 1 High    " },{ 0x0000, "91 Util Air/N2 Frit 1 High" },{ 0x0000, "92 MUG  N2 Frit 1 High    " },
{ 0x0000, "93 Fuel H2 Frit 1 Low     " },{ 0x0000, "94 Util Air/N2 Frit 1 Low " },{ 0x0000, "95 MUG  N2 Frit 1 Low     " },
{ 0x0000, "96 Fuel H2 Frit 2 High    " },{ 0x0000, "97 Util Air/N2 Frit 2 High" },{ 0x0000, "98 MUG  N2 Frit 2 High    " },
{ 0x0000, "99 Fuel H2 Frit 2 Low     " },{ 0x0000, "9a Util Air/N2 Frit 2 Low " },{ 0x0000, "9b MUG  N2 Frit 2 Low     " },

{ 0x0000, "9c Fuel H2 Max Flow       " },{ 0x0000, "9d Util Air/N2 Max Flow   " },{ 0x0000, "9e MUG  N2 Max Flow       " },
{ 0x0000, "9f                        " },{ 0x0000, "a0 Util O2/He Frit 1 High " },{ 0x0000, "a1 MUG  He Frit 1 High    " },
{ 0x0000, "a2                        " },{ 0x0000, "a3 Util O2/He Frit 1 Low  " },{ 0x0000, "a4 MUG  He Frit 1 Low     " },
{ 0x0000, "a5                        " },{ 0x0000, "a6 Util O2/He Frit 2 High " },{ 0x0000, "a7 MUG  He Frit 2 High    " },

{ 0x0000, "a8                        " },{ 0x0000, "a9 Util O2/He Frit 2 Low  " },{ 0x0000, "aa MUG  He Frit 2 Low     " },
{ 0x0000, "ab                        " },{ 0x0000, "ac Util O2/He Max Flow    " },{ 0x0000, "ad MUG  He Max Flow       " },
{ 0x0000, "ae                        " },{ 0x0000, "af Util H2/ArMe Frit1 High" },{ 0x0000, "b0 MUG  H2/ArMe Frit1 High" },
{ 0x0000, "b1                        " },{ 0x0000, "b2 Util H2/ArMe Frit1 Low " },{ 0x0000, "b3 MUG  H2/ArMe Frit1 Low " },

{ 0x0000, "b4                        " },{ 0x0000, "b5 Util H2/ArMe Frit2 High" },{ 0x0000, "b6 MUG  H2/ArMe Frit2 High" },
{ 0x0000, "b7                        " },{ 0x0000, "b8 Util H2/ArMe Frit2 Low " },{ 0x0000, "b9 MUG  H2/ArMe Frit2 Low " },
{ 0x0000, "ba OEM_RdyPoll&DiagFilters" },{ 0x0000, "bb Util H2/ArMe Max Flow  " },{ 0x0000, "bc MUG  H2/ArMe Max Flow  " },
{ 0x0000, "bd OEM_Diag2_Offset_Valu  " },{ 0x0000, "be Util Ar Frit 1 High    " },{ 0x0000, "bf MUG  Ar Frit 1 High    " },

{ 0x0000, "c0 OEM_Diag2_Mlpyr&Divdr  " },{ 0x0000, "c1 Util Ar Frit 1 Low     " },{ 0x0000, "c2 MUG  Ar Frit 1 Low     " },
{ 0x0000, "c3 OEM_Diag1_Offset_Valu  " },{ 0x0000, "c4 Util Ar Frit 2 High    " },{ 0x0000, "c5 MUG  Ar Frit 2 High    " },
{ 0x0000, "c6 OEM_Diag1_Mlpyr&Divdr  " },{ 0x0000, "c7 Util Ar Frit 2 Low     " },{ 0x0000, "c8 MUG  Ar Frit 2 Low     " },
{ 0x0000, "c9 Chan 2 Min Flow        " },{ 0x0000, "ca Util Ar Max Flow       " },{ 0x0000, "cb MUG  Ar Max Flow       " },

 /* Gas 1  label */
{ 0x0000, "cc Gas1 label bytes 0,1   " },{ 0x0000, "cd Gas1 label bytes 2,3   " },{ 0x0000, "ce Gas1 label bytes 4,5   " },
{ 0x0000, "cf Gas1 label bytes 6,7   " },


  /* Gas 2 label */
{ 0x0000, "d0 Gas2 label bytes 0,1   " },{ 0x0000, "d1 Gas2 label bytes 2,3   " },{ 0x0000, "d2 Gas2 label bytes 4,5   " },
{ 0x0000, "d3 Gas2 label bytes 6,7   " },

  /* Gas 3 label */
{ 0x0000, "d4 Gas3 label bytes 0,1   " },{ 0x0000, "d5 Gas3 label bytes 2,3   " },{ 0x0000, "d6 Gas3 label bytes 4,5   " },
{ 0x0000, "d7 Gas3 label bytes 6,7   " },


  /* control line 1 */
{ 0x0000, "d8 Ctl1 label bytes 0,1   " },{ 0x0000, "d9 Ctl1 label bytes 2,3   " },{ 0x0000, "da Ctl1 label bytes 4,5   " },
{ 0x0000, "db Ctl1 label bytes 6,7   " },{ 0x0000, "dc Ctl1 label bytes 8,9   " },{ 0x0000, "dd Ctl1 label bytes 10,11 " },

  /* control line 2 */
{ 0x0000, "de Ctl2 label bytes 0,1   " },{ 0x0000, "df Ctl2 label bytes 2,3   " },{ 0x0000, "e0 Ctl2 label bytes 4,5   " },
{ 0x0000, "e1 Ctl2 label bytes 6,7   " },{ 0x0000, "e2 Ctl2 label bytes 8,9   " },{ 0x0000, "e3 Ctl2 label bytes 10,11 " },

  /* control line 3 */
{ 0x0000, "e4 Ctl3 label bytes 0,1   " },{ 0x0000, "e5 Ctl3 label bytes 2,3   " },{ 0x0000, "e6 Ctl3 label bytes 4,5   " },
{ 0x0000, "e7 Ctl3 label bytes 6,7   " },{ 0x0000, "e8 Ctl3 label bytes 8,9   " },{ 0x0000, "e9 Ctl3 label bytes 10,11 " },

  /* control line 4 */
{ 0x0000, "ea Ctl4 label bytes 0,1   " },{ 0x0000, "eb Ctl4 label bytes 2,3   " },{ 0x0000, "ec Ctl4 label bytes 4,5   " },
{ 0x0000, "ed Ctl4 label bytes 6,7   " },{ 0x0000, "ee Ctl4 label bytes 8,9   " },{ 0x0000, "ef Ctl4 label bytes 10,11 " },

{ 0x0000, "f0                        " },{ 0x0000, "f1                        " },{ 0x0000, "f2                        " },
{ 0x0000, "f3                        " },{ 0x0000, "f4                        " },{ 0x0000, "f5 CPF_Pneu_Flag;must = 0 " },

  /* thermal constants */
{ 0x0000, "f6 Thermal1: P, I         " },{ 0x0000, "f7 Thermal2: D,minTemp    " },{ 0x0000, "f8 Thermal3:maxT,snsr,freq" },
{ 0x0000, "f9 Thermal4: rate,cyo,conf" },{ 0x0000, "fa Thermal5: div-P,I,D    " },{ 0x0000, "fb Thermal6: CtlErr,IntPwr" },

  /* detector id */
{ 0x0000, "fc Det Title  bytes 0,1   " },{ 0x0000, "fd Det title  bytes 2,3   " },{ 0x0000, "fe Det title  bytes 4,5   " },

{ 0x0000, "ff Test Station ID Number " } 

/*#!inc_addr*/

};



static const U_INT16 Factory_Aux_Pid_Address_Table[8/*gas*/][4/*term*/][3/*ch*/]= {

  /* normal aux mapping */
{{ FACT_CH_1_N2_I_GAIN,         FACT_CH_2_N2_I_GAIN,    FACT_CH_3_N2_I_GAIN     },
 { FACT_CH_1_N2_P_GAIN,         FACT_CH_2_N2_P_GAIN,    FACT_CH_3_N2_P_GAIN     },
 { FACT_CH_1_N2_D_GAIN,         FACT_CH_2_N2_D_GAIN,    FACT_CH_3_N2_D_GAIN     },
 { FACT_CH_1_N2_NL_GAIN,        FACT_CH_2_N2_NL_GAIN,   FACT_CH_3_N2_NL_GAIN    }},

{{ FACT_CH_1_H2_I_GAIN,         FACT_CH_2_H2_I_GAIN,    FACT_CH_3_H2_I_GAIN     },
 { FACT_CH_1_H2_P_GAIN,         FACT_CH_2_H2_P_GAIN,    FACT_CH_3_H2_P_GAIN     },
 { FACT_CH_1_H2_D_GAIN,         FACT_CH_2_H2_D_GAIN,    FACT_CH_3_H2_D_GAIN     },
 { FACT_CH_1_H2_NL_GAIN,        FACT_CH_2_H2_NL_GAIN,   FACT_CH_3_H2_NL_GAIN    }},

{{ FACT_CH_1_He_I_GAIN,         FACT_CH_2_He_I_GAIN,    FACT_CH_3_He_I_GAIN     },
 { FACT_CH_1_He_P_GAIN,         FACT_CH_2_He_P_GAIN,    FACT_CH_3_He_P_GAIN     },
 { FACT_CH_1_He_D_GAIN,         FACT_CH_2_He_D_GAIN,    FACT_CH_3_He_D_GAIN     },
 { FACT_CH_1_He_NL_GAIN,        FACT_CH_2_He_NL_GAIN,   FACT_CH_3_He_NL_GAIN    }},

{{ FACT_CH_1_ArMe_I_GAIN,       FACT_CH_2_ArMe_I_GAIN,  FACT_CH_3_ArMe_I_GAIN   },
 { FACT_CH_1_ArMe_P_GAIN,       FACT_CH_2_ArMe_P_GAIN,  FACT_CH_3_ArMe_P_GAIN   },
 { FACT_CH_1_ArMe_D_GAIN,       FACT_CH_2_ArMe_D_GAIN,  FACT_CH_3_ArMe_D_GAIN   },
 { FACT_CH_1_ArMe_NL_GAIN,      FACT_CH_2_ArMe_NL_GAIN, FACT_CH_3_ArMe_NL_GAIN  }},

{{              0,                      0,                      0               },
 {              0,                      0,                      0               },
 {              0,                      0,                      0               },
 {              0,                      0,                      0               }},

{{ FACT_CH_1_N2_I_GAIN,         FACT_CH_2_N2_I_GAIN,    FACT_CH_3_N2_I_GAIN,    },
 { FACT_CH_1_N2_P_GAIN,         FACT_CH_2_N2_P_GAIN,    FACT_CH_3_N2_P_GAIN,    },
 { FACT_CH_1_N2_D_GAIN,         FACT_CH_2_N2_D_GAIN,    FACT_CH_3_N2_D_GAIN,    },
 { FACT_CH_1_N2_NL_GAIN,        FACT_CH_2_N2_NL_GAIN,   FACT_CH_3_N2_NL_GAIN,   }},

{{              0,                      0,                      0               },
 {              0,                      0,                      0               },
 {              0,                      0,                      0               },
 {              0,                      0,                      0               }},

{{              0,                      0,                      0               },
 {              0,                      0,                      0               },
 {              0,                      0,                      0               },
 {              0,                      0,                      0               }} };

static const U_INT16 User_Aux_Pid_Address_Table[/*gas*/][4/*term*/][3/*ch*/]= {

{{ USER_CH_1_N2_I_GAIN,         USER_CH_2_N2_I_GAIN,    USER_CH_3_N2_I_GAIN     },
 { USER_CH_1_N2_P_GAIN,         USER_CH_2_N2_P_GAIN,    USER_CH_3_N2_P_GAIN     },
 { USER_CH_1_N2_D_GAIN,         USER_CH_2_N2_D_GAIN,    USER_CH_3_N2_D_GAIN     },
 { USER_CH_1_N2_NL_GAIN,        USER_CH_2_N2_NL_GAIN,   USER_CH_3_N2_NL_GAIN    }},

{{ USER_CH_1_H2_I_GAIN,         USER_CH_2_H2_I_GAIN,    USER_CH_3_H2_I_GAIN     },
 { USER_CH_1_H2_P_GAIN,         USER_CH_2_H2_P_GAIN,    USER_CH_3_H2_P_GAIN     },
 { USER_CH_1_H2_D_GAIN,         USER_CH_2_H2_D_GAIN,    USER_CH_3_H2_D_GAIN     },
 { USER_CH_1_H2_NL_GAIN,        USER_CH_2_H2_NL_GAIN,   USER_CH_3_H2_NL_GAIN    }},

{{ USER_CH_1_He_I_GAIN,         USER_CH_2_He_I_GAIN,    USER_CH_3_He_I_GAIN     },
 { USER_CH_1_He_P_GAIN,         USER_CH_2_He_P_GAIN,    USER_CH_3_He_P_GAIN     },
 { USER_CH_1_He_D_GAIN,         USER_CH_2_He_D_GAIN,    USER_CH_3_He_D_GAIN     },
 { USER_CH_1_He_NL_GAIN,        USER_CH_2_He_NL_GAIN,   USER_CH_3_He_NL_GAIN    }},

{{ USER_CH_1_ArMe_I_GAIN,       USER_CH_2_ArMe_I_GAIN,  USER_CH_3_ArMe_I_GAIN   },
 { USER_CH_1_ArMe_P_GAIN,       USER_CH_2_ArMe_P_GAIN,  USER_CH_3_ArMe_P_GAIN   },
 { USER_CH_1_ArMe_D_GAIN,       USER_CH_2_ArMe_D_GAIN,  USER_CH_3_ArMe_D_GAIN   },
 { USER_CH_1_ArMe_NL_GAIN,      USER_CH_2_ArMe_NL_GAIN, USER_CH_3_ArMe_NL_GAIN  }},

{{              0,      	        0,                      0               },
 {              0,      	        0,                      0               },
 {              0,      	        0,                      0               },
 {              0,      	        0,                      0               }},

{{ USER_CH_1_N2_I_GAIN, 	USER_CH_2_N2_I_GAIN,    USER_CH_3_N2_I_GAIN	},
 { USER_CH_1_N2_P_GAIN, 	USER_CH_2_N2_P_GAIN,    USER_CH_3_N2_P_GAIN	},
 { USER_CH_1_N2_D_GAIN, 	USER_CH_2_N2_D_GAIN,    USER_CH_3_N2_D_GAIN     },
 { USER_CH_1_N2_NL_GAIN,	USER_CH_2_N2_NL_GAIN,   USER_CH_3_N2_NL_GAIN    }},

{{              0,      	        0,                      0               },
 {              0,      	        0,                      0               },
 {              0,      	        0,                      0               },
 {              0,      	        0,                      0               }},

{{              0,                      0,                      0               },
 {              0,                      0,                      0               },
 {              0,                      0,                      0               },
 {              0,                      0,                      0               }}
 };


/**begin_proto**/

U_INT16 GetFactAuxPidEepromAddress( GAS_TYPE gas, U_INT8 sensor_position, U_INT8 term )

/**end_proto**/
{
    return Factory_Aux_Pid_Address_Table [ (U_INT8) gas ] [ term ] [ sensor_position -1 ];
}

/**begin_proto**/

U_INT16 GetUserAuxPidEepromAddress( GAS_TYPE gas, U_INT8 sensor_position, U_INT8 term )

/**end_proto**/
{
    return User_Aux_Pid_Address_Table [ (U_INT8) gas ] [ term ] [ sensor_position -1 ];
}

extern U_INT16 EEPROM_Write_Address;
extern U_INT16 EEPROM_Write_Data;

/**begin_proto**/

BIT8 InitAuxEEPROM ( U_INT8 module, U_INT16 type, U_INT16 range, U_INT16 scale, U_INT16 snh, U_INT16 snl, U_INT16 bd )

/**end_proto**/

{

     U_INT16 i;
     BIT8    ok;

     ok = TRUE;


     for ( i = 0; i< AUX_EEPROM_SIZE; i++ )
     {
          if ( i == CHECKSUM ) continue;

          EEPROM_Write_Address = i;
          EEPROM_Write_Data    = Aux_EEPROM_Default [ i ].value;
          ok &= WriteEeprom ( module, i, Aux_EEPROM_Default[ i ].value );
     }

     WriteAuxPIDs( module );

     type = type | ((U_INT16)AUX_MODULE << 8);

     EEPROM_Write_Address = TYPE;
     EEPROM_Write_Data    = type;
     ok &= WriteEeprom ( module, EEPROM_Write_Address, EEPROM_Write_Data );

     EEPROM_Write_Address = PS_RANGE+0;
     EEPROM_Write_Data    = range;
     ok &= WriteEeprom ( module, EEPROM_Write_Address, EEPROM_Write_Data );

     EEPROM_Write_Address = P_SCALING+0;
     EEPROM_Write_Data    = scale;
     ok &= WriteEeprom ( module, EEPROM_Write_Address, EEPROM_Write_Data );

     EEPROM_Write_Address = PS_RANGE+1; 
     EEPROM_Write_Data    = range;
     ok &= WriteEeprom ( module, EEPROM_Write_Address, EEPROM_Write_Data );

     EEPROM_Write_Address = P_SCALING+1;
     EEPROM_Write_Data    = scale;
     ok &= WriteEeprom ( module, EEPROM_Write_Address, EEPROM_Write_Data );

     EEPROM_Write_Address = PS_RANGE+2;
     EEPROM_Write_Data    = range;
     ok &= WriteEeprom ( module, EEPROM_Write_Address, EEPROM_Write_Data );

     EEPROM_Write_Address = P_SCALING+2;
     EEPROM_Write_Data    = scale;
     ok &= WriteEeprom ( module, EEPROM_Write_Address, EEPROM_Write_Data );



     ok &= WriteEeprom( module, BUILD_DATE, bd );
     ok &= WriteEeprom( module, SERIAL_NO_HIGH, snh );
     ok &= WriteEeprom( module, SERIAL_NO_LOW,  snl );

     return ok;

}


static const UPDATE_TABLE_ENTRY  INIT_AUX_PID_TABLE[] = {

 { INIT_PID,  0x004b, FACT_CH_1_N2_I_GAIN, 	1},
 { INIT_PID,  0x0029, FACT_CH_1_N2_P_GAIN, 	1},
 { INIT_PID,  0x001f, FACT_CH_1_N2_D_GAIN, 	1},
 { INIT_PID,  0xffff, FACT_CH_1_N2_NL_GAIN, 	1},

 { INIT_PID,  0x004b, FACT_CH_2_N2_I_GAIN, 	2},
 { INIT_PID,  0x0029, FACT_CH_2_N2_P_GAIN, 	2},
 { INIT_PID,  0x001f, FACT_CH_2_N2_D_GAIN, 	2},
 { INIT_PID,  0xffff, FACT_CH_2_N2_NL_GAIN, 	2},

 { INIT_PID,  0x004b, FACT_CH_3_N2_I_GAIN, 	3},
 { INIT_PID,  0x0029, FACT_CH_3_N2_P_GAIN, 	3},
 { INIT_PID,  0x001f, FACT_CH_3_N2_D_GAIN, 	3},
 { INIT_PID,  0xffff, FACT_CH_3_N2_NL_GAIN, 	3},

 { INIT_PID,  0x0025, FACT_CH_1_H2_I_GAIN, 	1},
 { INIT_PID,  0x0040, FACT_CH_1_H2_P_GAIN, 	1},
 { INIT_PID,  0x005d, FACT_CH_1_H2_D_GAIN, 	1},
 { INIT_PID,  0xffff, FACT_CH_1_H2_NL_GAIN, 	1},

 { INIT_PID,  0x0025, FACT_CH_2_H2_I_GAIN, 	2},
 { INIT_PID,  0x0040, FACT_CH_2_H2_P_GAIN, 	2},
 { INIT_PID,  0x005d, FACT_CH_2_H2_D_GAIN, 	2},
 { INIT_PID,  0xffff, FACT_CH_2_H2_NL_GAIN, 	2},

 { INIT_PID,  0x0025, FACT_CH_3_H2_I_GAIN, 	3},
 { INIT_PID,  0x0040, FACT_CH_3_H2_P_GAIN, 	3},
 { INIT_PID,  0x005d, FACT_CH_3_H2_D_GAIN, 	3},
 { INIT_PID,  0xffff, FACT_CH_3_H2_NL_GAIN, 	3},

 { INIT_PID,  0x002a, FACT_CH_1_He_I_GAIN, 	1},
 { INIT_PID,  0x0028, FACT_CH_1_He_P_GAIN, 	1},
 { INIT_PID,  0x0021, FACT_CH_1_He_D_GAIN, 	1},
 { INIT_PID,  0xffff, FACT_CH_1_He_NL_GAIN, 	1},

 { INIT_PID,  0x002a, FACT_CH_2_He_I_GAIN, 	2},
 { INIT_PID,  0x0028, FACT_CH_2_He_P_GAIN, 	2},
 { INIT_PID,  0x0021, FACT_CH_2_He_D_GAIN, 	2},
 { INIT_PID,  0xffff, FACT_CH_2_He_NL_GAIN, 	2},

 { INIT_PID,  0x002a, FACT_CH_3_He_I_GAIN, 	3},
 { INIT_PID,  0x0028, FACT_CH_3_He_P_GAIN, 	3},
 { INIT_PID,  0x0021, FACT_CH_3_He_D_GAIN, 	3},
 { INIT_PID,  0xffff, FACT_CH_3_He_NL_GAIN, 	3},

 { INIT_PID,  0x0052, FACT_CH_1_ArMe_I_GAIN, 	1},
 { INIT_PID,  0x003f, FACT_CH_1_ArMe_P_GAIN, 	1},
 { INIT_PID,  0x0034, FACT_CH_1_ArMe_D_GAIN, 	1},
 { INIT_PID,  0xffff, FACT_CH_1_ArMe_NL_GAIN, 	1},

 { INIT_PID,  0x0052, FACT_CH_2_ArMe_I_GAIN, 	2},
 { INIT_PID,  0x003f, FACT_CH_2_ArMe_P_GAIN, 	2},
 { INIT_PID,  0x0034, FACT_CH_2_ArMe_D_GAIN, 	2},
 { INIT_PID,  0xffff, FACT_CH_2_ArMe_NL_GAIN, 	2},

 { INIT_PID,  0x0052, FACT_CH_3_ArMe_I_GAIN, 	3},
 { INIT_PID,  0x003f, FACT_CH_3_ArMe_P_GAIN, 	3},
 { INIT_PID,  0x0034, FACT_CH_3_ArMe_D_GAIN, 	3},
 { INIT_PID,  0xffff, FACT_CH_3_ArMe_NL_GAIN, 	3},

 { INIT_PID,  0x0000, USER_CH_1_N2_I_GAIN, 	1},
 { INIT_PID,  0x0000, USER_CH_1_N2_P_GAIN, 	1},
 { INIT_PID,  0x0000, USER_CH_1_N2_D_GAIN, 	1},
 { INIT_PID,  0xffff, USER_CH_1_N2_NL_GAIN, 	1},

 { INIT_PID,  0x0000, USER_CH_2_N2_I_GAIN, 	2},
 { INIT_PID,  0x0000, USER_CH_2_N2_P_GAIN, 	2},
 { INIT_PID,  0x0000, USER_CH_2_N2_D_GAIN, 	2},
 { INIT_PID,  0xffff, USER_CH_2_N2_NL_GAIN, 	2},

 { INIT_PID,  0x0000, USER_CH_3_N2_I_GAIN, 	3},
 { INIT_PID,  0x0000, USER_CH_3_N2_P_GAIN, 	3},
 { INIT_PID,  0x0000, USER_CH_3_N2_D_GAIN, 	3},
 { INIT_PID,  0xffff, USER_CH_3_N2_NL_GAIN, 	3},

 { INIT_PID,  0x0000, USER_CH_1_H2_I_GAIN, 	1},
 { INIT_PID,  0x0000, USER_CH_1_H2_P_GAIN, 	1},
 { INIT_PID,  0x0000, USER_CH_1_H2_D_GAIN, 	1},
 { INIT_PID,  0xffff, USER_CH_1_H2_NL_GAIN, 	1},

 { INIT_PID,  0x0000, USER_CH_2_H2_I_GAIN, 	2},
 { INIT_PID,  0x0000, USER_CH_2_H2_P_GAIN, 	2},
 { INIT_PID,  0x0000, USER_CH_2_H2_D_GAIN, 	2},
 { INIT_PID,  0xffff, USER_CH_2_H2_NL_GAIN, 	2},

 { INIT_PID,  0x0000, USER_CH_3_H2_I_GAIN, 	3},
 { INIT_PID,  0x0000, USER_CH_3_H2_P_GAIN, 	3},
 { INIT_PID,  0x0000, USER_CH_3_H2_D_GAIN, 	3},
 { INIT_PID,  0xffff, USER_CH_3_H2_NL_GAIN, 	3},

 { INIT_PID,  0x0000, USER_CH_1_He_I_GAIN, 	1},
 { INIT_PID,  0x0000, USER_CH_1_He_P_GAIN, 	1},
 { INIT_PID,  0x0000, USER_CH_1_He_D_GAIN, 	1},
 { INIT_PID,  0xffff, USER_CH_1_He_NL_GAIN, 	1},

 { INIT_PID,  0x0000, USER_CH_2_He_I_GAIN, 	2},
 { INIT_PID,  0x0000, USER_CH_2_He_P_GAIN, 	2},
 { INIT_PID,  0x0000, USER_CH_2_He_D_GAIN, 	2},
 { INIT_PID,  0xffff, USER_CH_2_He_NL_GAIN, 	2},

 { INIT_PID,  0x0000, USER_CH_3_He_I_GAIN, 	3},
 { INIT_PID,  0x0000, USER_CH_3_He_P_GAIN, 	3},
 { INIT_PID,  0x0000, USER_CH_3_He_D_GAIN, 	3},
 { INIT_PID,  0xffff, USER_CH_3_He_NL_GAIN, 	3},

 { INIT_PID,  0x0000, USER_CH_1_ArMe_I_GAIN, 	1},
 { INIT_PID,  0x0000, USER_CH_1_ArMe_P_GAIN, 	1},
 { INIT_PID,  0x0000, USER_CH_1_ArMe_D_GAIN, 	1},
 { INIT_PID,  0xffff, USER_CH_1_ArMe_NL_GAIN, 	1},

 { INIT_PID,  0x0000, USER_CH_2_ArMe_I_GAIN, 	2},
 { INIT_PID,  0x0000, USER_CH_2_ArMe_P_GAIN, 	2},
 { INIT_PID,  0x0000, USER_CH_2_ArMe_D_GAIN, 	2},
 { INIT_PID,  0xffff, USER_CH_2_ArMe_NL_GAIN, 	2},

 { INIT_PID,  0x0000, USER_CH_3_ArMe_I_GAIN, 	3},
 { INIT_PID,  0x0000, USER_CH_3_ArMe_P_GAIN, 	3},
 { INIT_PID,  0x0000, USER_CH_3_ArMe_D_GAIN, 	3},
 { INIT_PID,  0xffff, USER_CH_3_ArMe_NL_GAIN, 	3},

 { END,         0,	0,	0}  };


static const UPDATE_TABLE_ENTRY  A_0109_Update[] = {
  { INIT_WORD, 0x0109, 0x01,   0 },      /* update the version field */
  { END,       0,      0,      0 } };

/**begin_proto**/

void UpdateAuxEeprom109(  U_INT8 modul_nmbr )         

/**end_proto**/
{

   WriteAuxPIDs( modul_nmbr );
   UpdateEeprom( modul_nmbr, (pUPDATE_TABLE)A_0109_Update );

}



static const UPDATE_TABLE_ENTRY  Aux_Ps_Range_Update[] = {


  { SAVE_WORD,  0x01,   0,      0 },       /* scribble version in case of failure */
  { INIT_WORD,  0xff0b, 0x01,   0 },

  { SKIP_NE,    0x0003, 0x09,   2 },       /* if PSensorRange == 15 PSI */
  {  INIT_WORD, 0x327f, 0x09,   0 },           /* set range in IU */
  {  INIT_WORD, 0x0050, 0x18,   0 },           /* set dynes/cm2 */

  { SKIP_NE,    0x0001, 0x09,   2 },       /* if PSensorRange == 100 PSI */
  {  INIT_WORD, 0xa854, 0x09,   0 },           /* set range to 100 psi in IU */
  {  INIT_WORD, 0x00a0, 0x18,   0 },           /* set dynes/cm2 */


  { SKIP_NE,    0x0003, 0x0a,   2 },       /* if PSensorRange == 15 PSI */
  {  INIT_WORD, 0x327f, 0x0a,   0 },           /* set range in IU */
  {  INIT_WORD, 0x0050, 0x19,   0 },           /* set dynes/cm2 */

  { SKIP_NE,    0x0001, 0x0a,   2 },       /* if PSensorRange == 100 PSI */
  {  INIT_WORD, 0xa854, 0x0a,   0 },           /* set range to 100 psi in IU */
  {  INIT_WORD, 0x00a0, 0x19,   0 },           /* set dynes/cm2 */


  { SKIP_NE,    0x0003, 0x0b,   2 },       /* if PSensorRange == 15 PSI */
  {  INIT_WORD, 0x327f, 0x0b,   0 },           /* set range in IU */
  {  INIT_WORD, 0x0050, 0x1a,   0 },           /* set dynes/cm2 */

  { SKIP_NE,    0x0001, 0x0b,   2 },       /* if PSensorRange == 100 PSI */
  {  INIT_WORD, 0xa854, 0x0b,   0 },           /* set range to 100 psi in IU */
  {  INIT_WORD, 0x00a0, 0x1a,   0 },           /* set dynes/cm2 */

  { RESTORE_WORD,0,     0x01,   0 },       /* rewrite version  */

  { END,        0,      0,      0 } };


 

/**begin_proto**/

void WriteAuxPsRange( U_INT8 module_number )

/**end_proto**/
{
   UpdateEeprom( module_number, (pUPDATE_TABLE)Aux_Ps_Range_Update );
}


static const UPDATE_TABLE_ENTRY  A_010A_Update[] = {

  { SAVE_WORD, 0x02,   0,      0 },       /* save the module type word */

  { INIT_WORD, 0xffff, 0x02,   0 },       /* scribble module type in case of failure */

  { COPY_BLOCK, 0x2a,  0x30,  96 },       /* pid terms */

  { COPY_BLOCK, 0x18,  0x1e,  18 },       /* calib constants */

  { INIT_WORD, 0x010a, 0x01,   0 },      /* write the new version code */

  { RESTORE_WORD, 0,   0x02,   0 },      /* return the module type word */

  { END,       0,      0,      0 } };



/**begin_proto**/

void UpdateAuxEeprom10A( U_INT8 modul_nmbr )       

/**end_proto**/
{

    UpdateEeprom( modul_nmbr, (pUPDATE_TABLE)A_010A_Update );

}



/**begin_proto**/

void WriteAuxPIDs ( U_INT8 module )

/**end_proto**/
{

   UpdateEeprom( module, (pUPDATE_TABLE)INIT_AUX_PID_TABLE );

}

/**begin_proto**/

UI_ERR GetAuxEepromDef (U_INT16 address, BIT8 *definition )

/**end_proto**/
{

    U_INT8 i;

    if ( address >= AUX_EEPROM_SIZE ) return INVALID_PARAM;

    for ( i = 0 ; i < EEPROM_DEF_SIZE ; i++ )  definition[i] = Aux_EEPROM_Default[address].definition[i];

    return OK;
}


static const UPDATE_TABLE_ENTRY  A_010B_Update[] = {

  { INIT_WORD, 0x010B, 0x01,   0 },      /* write the new version code */

  { END,       0,      0,      0 } };



/**begin_proto**/

void UpdateAuxEeprom10B( U_INT8 modul_nmbr )       

/**end_proto**/
{

    UpdateEeprom( modul_nmbr, (pUPDATE_TABLE)A_010B_Update );

    WriteAuxPsRange( modul_nmbr );
    WriteAuxPIDs   ( modul_nmbr );

}



static const UPDATE_TABLE_ENTRY  A_010C_Update[] = {

  { CALC_CKSUM, 0,      0,      0 },       /* calc and write the module cksum    */

  { INIT_WORD, 0x010C, 0x01,   0 },      /* write the new version code */

  { END,       0,      0,      0 } };



/**begin_proto**/

void UpdateAuxEeprom10C( U_INT8 modul_nmbr )       

/**end_proto**/
{

    UpdateEeprom( modul_nmbr, (pUPDATE_TABLE)A_010C_Update );

}



static const UPDATE_TABLE_ENTRY  A_010D_Update[] = {


  { INIT_WORD, 0x010d, 0x01,   0 },      /* write the new version code */

  { END,       0,      0,      0 } };



/**begin_proto**/

void UpdateAuxEeprom10D( U_INT8 modul_nmbr )       

/**end_proto**/
{

    UpdateEeprom( modul_nmbr, (pUPDATE_TABLE)A_010D_Update );

}


static const UPDATE_TABLE_ENTRY  A_010E_Update[] = {


  { INIT_WORD, 0x010e, 0x01,   0 },      /* write the new version code */

  { END,       0,      0,      0 } };



/**begin_proto**/

void UpdateAuxEeprom10E( U_INT8 modul_nmbr )       

/**end_proto**/
{

    UpdateEeprom( modul_nmbr, (pUPDATE_TABLE)A_010E_Update );

}


static const UPDATE_TABLE_ENTRY  A_0200_Update[] = {


  { INIT_WORD, 0x0200, 0x01,   0 },      /* write the new version code */

  { END,       0,      0,      0 } };



/**begin_proto**/

void UpdateAuxEeprom200( U_INT8 modul_nmbr )       

/**end_proto**/
{
    WriteAuxPIDs   ( modul_nmbr );

    UpdateEeprom( modul_nmbr, (pUPDATE_TABLE)A_0200_Update );
}

